//
//  UserInfoManager.m
//  FbeeAPP
//
//  Created by dev-m on 2018/3/12.
//  Copyright © 2018年 mohao. All rights reserved.
//

#import "UserInfoManager.h"

#define UserToken       @"_user_token"
#define UserId          @"_user_uid"
#define UserMobile      @"_user_mobile"
#define UserNickName    @"_user_nickName"
#define UserWalletAddr  @"_user_walletAddr"
#define UserTopicCount  @"_user_topicCount"
#define UserArea        @"_user_area"

@interface UserInfoManager()
{
    NSUserDefaults *userDefault;
}

@end

@implementation UserInfoManager

+(instancetype)sharedManager
{
    static dispatch_once_t queue;
    static UserInfoManager * manager = nil;
    
    dispatch_once(&queue, ^{
        manager = [[UserInfoManager alloc]init];
    });
    
    return manager;
}

-(instancetype)init
{
    self = [super init];
    
    if (self) {
        
        userDefault = [NSUserDefaults standardUserDefaults];
        
        _token = SafeGetStringValue([userDefault objectForKey:UserToken]);
        _userId = SafeGetStringValue([userDefault objectForKey:UserId]);
        _mobile = SafeGetStringValue([userDefault objectForKey:UserMobile]);
        _areaCode = SafeGetStringValue([userDefault objectForKey:UserArea]);
        _nickName = SafeGetStringValue([userDefault objectForKey:UserNickName]);
        _walletAddr = SafeGetStringValue([userDefault objectForKey:UserWalletAddr]);
        _topicCount = SafeGetStringValue([userDefault objectForKey:UserTopicCount]);
        
        if (![NSString isEmptyString:_walletAddr]) {
            _haveWallet = YES;
        }
        
        if (![NSString isEmptyString:_token] && ![NSString isEmptyString:_userId]) {
            _isLogin = YES;
        }

    }
    
    return self;
}

-(void)getUserInfo
{
    
}

-(void)setMobile:(NSString *)mobile
{
    [userDefault setObject:mobile forKey:UserMobile];
    [userDefault synchronize];
    _mobile = mobile;
}

-(void)setAreaCode:(NSString *)areaCode
{
    [userDefault setObject:areaCode forKey:UserArea];
    [userDefault synchronize];
    _areaCode = areaCode;
}

-(void)setNickName:(NSString *)nickName
{
    [userDefault setObject:nickName forKey:UserNickName];
    [userDefault synchronize];
    _nickName = nickName;
}

-(void)setWalletAddr:(NSString *)walletAddr
{
    _haveWallet = NO;
    
    if (![NSString isEmptyString:walletAddr]) {
        _haveWallet = YES;
    }
    
    [userDefault setObject:walletAddr forKey:UserWalletAddr];
    [userDefault synchronize];
    _walletAddr = walletAddr;
}

-(void)setTopicCount:(NSString *)topicCount
{
    [userDefault setObject:topicCount forKey:UserTopicCount];
    [userDefault synchronize];
    _topicCount = topicCount;
}

-(void)setLoginInfoByToken:(NSString *)t userId:(NSString *)uid
{
    [userDefault setObject:t forKey:UserToken];
    [userDefault setObject:uid forKey:UserId];
    [userDefault synchronize];
    
    _token = SafeGetStringValue([userDefault objectForKey:UserToken]);
    _userId = SafeGetStringValue([userDefault objectForKey:UserId]);
    
    if (![NSString isEmptyString:_token] && ![NSString isEmptyString:_userId]) {
        _isLogin = YES;
    }
    
}

-(void)logOut
{
    [userDefault setObject:@"" forKey:UserToken];
    [userDefault setObject:@"" forKey:UserId];
    [userDefault setObject:@"" forKey:UserMobile];
    [userDefault setObject:@"" forKey:UserArea];
    [userDefault setObject:@"" forKey:UserNickName];
    [userDefault setObject:@"" forKey:UserWalletAddr];
    [userDefault setObject:@"" forKey:UserTopicCount];
    [userDefault synchronize];
    
    _isLogin = NO;
    _haveWallet = NO;
    _token = @"";
    _userId = @"";
    _mobile = @"";
    _areaCode = @"";
    _nickName = @"";
    _walletAddr = @"";
    _topicCount = @"";
}

@end
